<?php
namespace Modules\Home\Controllers;

class PromotionController extends ControllerBase {
  public function initialize() {
    parent::initialize();

    $this -> _checkLogin();
    $this -> view -> setLayout('index');
  }

  public function indexAction() {
    $this -> tag -> prependTitle('我的推荐');

    $currentPage = abs($this -> request -> getQuery('page', 'int', 1));
    if($currentPage <= 0) {
      $currentPage = 1;
    }

    $sessionGetUser = $this -> session -> get('user');

    $users = $this -> modelsManager -> createBuilder()
      -> from([
        'u' => 'Common\\Models\\UserModel',
        'ui' => 'Common\\Models\\UserInfoModel',
      ])
      -> where('u.user_id = ui.user_id AND u.promotion_user_id = ' . $sessionGetUser['user_id'])
      -> columns(
        'u.user_code, u.status, u.user_level, u.real_name, '.
        'ui.create_time'
      )
      -> orderBy('u.user_id DESC');

    $pagerUsers = new \Phalcon\Paginator\Pager(new \Phalcon\Paginator\Adapter\QueryBuilder([
      'builder' => $users,
      'limit' => 10,
      'page' => $currentPage
    ]), [
      'layoutClass' => 'Modules\\Home\\Libraries\\Paginator\\Pager\\Layout\\Bootstrap',
      'rangeLength' => 5,
      'urlMask' => '?page={%page_number}'
    ]);

    $this -> view -> setVar('pagerUsers', $pagerUsers);
  }
}
?>